# Support libraries for KiCad

_realname=kicad
pkgbase=mingw-w64-${_realname}-library
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-footprints"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-symbols"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-templates"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-meta")
pkgver=6.0.10
pkgrel=1
pkgdesc="Support libraries for KiCad (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://www.kicad.org/'
license=("spdx:GPL-3.0-or-later")
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=("${MINGW_PACKAGE_PREFIX}-${_realname}")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('!strip')
source=("${_realname}-footprints-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-footprints/-/archive/${pkgver}/kicad-footprints-${pkgver}.tar.gz"
        "${_realname}-symbols-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-symbols/-/archive/${pkgver}/kicad-symbols-${pkgver}.tar.gz"
        "${_realname}-templates-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-templates/-/archive/${pkgver}/kicad-templates-${pkgver}.tar.gz"
        "${_realname}-packages3D-${pkgver}.tar.gz"::"https://gitlab.com/kicad/libraries/kicad-packages3D/-/archive/${pkgver}/kicad-packages3D-${pkgver}.tar.gz")
sha256sums=('cce34fe9dfb09380a494e840f6405261147b42196a1187da2fa89189b246d12f'
            '472aa96b296e87633f5076316ca8bcf0351ba0d890da8de8a693aba381c156dc'
            'c85d80667db00423c9fe809a0ed88d6f0f431e026310f1903fdb072806d1e2e8'
            'c7903ddfbabbccf6b8c0fd2e5f24fd301dfe493a0fc883998908c6d65a407564')

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  for _s in "footprints" "symbols" "templates" "packages3D"; do
    msg2 "Build kicad-${_s}"
    [[ -d "${srcdir}"/build-${_s}-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${_s}-${MSYSTEM}
    mkdir -p "${srcdir}/build-${_s}-${MSYSTEM}" && cd "${srcdir}/build-${_s}-${MSYSTEM}"

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      "${MINGW_PREFIX}"/bin/cmake.exe \
        -GNinja \
        -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
        "${extra_config[@]}" \
        ../${_realname}-${_s}-${pkgver}

    "${MINGW_PREFIX}"/bin/cmake.exe --build .
  done
}

package_footprints() {
  pkgdesc="KiCad footprint libraries (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-footprints-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-footprints-git)
  depends=()
  optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd "${srcdir}/build-footprints-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_symbols() {
  pkgdesc="KiCad symbol libraries (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-symbols-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-symbols-git)
  depends=()
  optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd "${srcdir}/build-symbols-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_templates() {
  pkgdesc="KiCad template libraries (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-templates-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-templates-git)

  cd "${srcdir}/build-templates-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_packages3D() {
  pkgdesc="KiCad 3D model libraries (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D-git)

  cd "${srcdir}/build-packages3D-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_meta() {
  pkgdesc="Meta package for KiCad containing the core application and support libraries (mingw-w64)"

  depends=(${MINGW_PACKAGE_PREFIX}-${_realname}
           ${MINGW_PACKAGE_PREFIX}-${_realname}-footprints
           ${MINGW_PACKAGE_PREFIX}-${_realname}-symbols
           ${MINGW_PACKAGE_PREFIX}-${_realname}-templates
           ${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D)
  optdepends=()
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-${_realname}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
